package com.longrise.android.byjk.plugins.dealsituation.course.video2.education.assist;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.android.byjk.model.CourseTrainDirBean;
import com.longrise.android.byjk.plugins.course.coursedetail.courseadapter.AssistAdapter;

import java.util.List;

/**
 * Created by godliness on 2017/9/13.
 * 辅助操作CourseCache
 */

public final class AssistCache {

    /**
     * @param courseId 课程id
     * @param position 索引位置
     *                 根据索引位置更新课件学习状态
     */
    public static boolean updateCwidStateInCache(String courseId, int[] position) {
        try {
            String value = CourseCache.cacheFromLocal(courseId, CourseCache.Flag.FLAG_FROM_COURSE_DETAIL);
            if (value == null) {
                return false;
            }
            Gson gson = new Gson();
            CourseTrainDirBean mCourseTrainDirBean = gson.fromJson(value, CourseTrainDirBean.class);
            List<CourseTrainDirBean.Dirinfo> dirinfos = mCourseTrainDirBean.result.traindirstruct.dirinfo;
            boolean isUpdated = AssistAdapter.assistUpdateCacheCwidState(dirinfos, position);
            if (!isUpdated) {
                return false;
            }
            String updateValue = JSONSerializer.getInstance().Serialize(mCourseTrainDirBean);
            if (!TextUtils.isEmpty(updateValue)) {
                CourseCache.cacheToLocal(courseId, updateValue, CourseCache.Flag.FLAG_FROM_COURSE_DETAIL);

                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
